package com.yangchoi.jetpackcompose.ui.main.viewmodel

import android.util.Log
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
import com.yangchoi.lib_base.base.BaseViewModel
import com.yangchoi.lib_base.network.ApiService
import com.yangchoi.lib_base.network.entity.ArticleEntity
import com.yangchoi.lib_base.network.entity.ProjectTreeItemBean
import dagger.hilt.android.lifecycle.HiltViewModel
import javax.inject.Inject

@HiltViewModel
class ProjectViewModel @Inject constructor(private val apiService: ApiService) : BaseViewModel() {

    var viewState by mutableStateOf(ProjectState())
        set

    fun dispatch(event: ProjectEvent,currentPage:Int,cid:Int = -1,onRefreshFinish:(()->Unit)? = null){
        when(event){
            ProjectEvent.OnInitProject -> {
                launchFlow(
                    requestBlock = { apiService.getProjectTreeList()},
                    response = {
                        it?.let {
                            viewState = viewState.copy(treeList = it as ArrayList<ProjectTreeItemBean>, loadCid = it[0].id)

                            launchFlow(
                                requestBlock = { apiService.getProjectListById(page = currentPage, cid = viewState.loadCid)},
                                response = {
                                    it?.let {
                                        viewState = viewState.copy(projectList = it.datas as ArrayList<ArticleEntity>)
                                    }
                                }
                            )
                        }
                    }
                )
            }

            ProjectEvent.OnGetProjectById -> {
                viewState = viewState.copy(loadCid = cid)
                launchFlow(
                    requestBlock = { apiService.getProjectListById(page = currentPage, cid = viewState.loadCid)},
                    response = {
                        onRefreshFinish?.invoke()
                        it?.let {
                            viewState = viewState.copy(projectList = it.datas as ArrayList<ArticleEntity>)
                        }
                    }
                )
            }

            ProjectEvent.OnUpdatePage -> {
                launchFlow(
                    requestBlock = { apiService.getProjectListById(page = currentPage, cid = viewState.loadCid)},
                    response = {
                        Log.e("RefreshTAG","结束")
                        onRefreshFinish?.invoke()
                        it?.let {
                            viewState = if (currentPage == 1){
                                viewState.copy(projectList = it.datas as ArrayList<ArticleEntity>)
                            }else{
                                viewState.copy(projectList = viewState.projectList?.toMutableList()?.apply {
                                    addAll(it.datas)
                                } as ArrayList<ArticleEntity>)
                            }

                        }
                    }
                )
            }
        }
    }

}

data class ProjectState(
    var treeList: ArrayList<ProjectTreeItemBean>? = null,
    var loadCid:Int = -1,
    var projectList: ArrayList<ArticleEntity>? = null
)

sealed class ProjectEvent{
    object OnInitProject : ProjectEvent()
    object OnGetProjectById : ProjectEvent()
    object OnUpdatePage : ProjectEvent()
}